var pageii = null;
var grid = null;
$(function() {
	
	grid = lyGrid({
		pagId : 'paging',
		l_column : [ {
			colkey : "aId",
			name : "id",
		}, {
			colkey : "aName",
			name : "广告名",
			isSort:true,
		}, {
			colkey : "img",
			name : "广告图片",
			renderData : function(rowindex,data, rowdata, column) {
				return "<img alt=''  src='"+data+"' height='20px'>";
			}
		},{
			colkey : "url",
			name : "广告链接",
			renderData : function(rowindex,data, rowdata, column) {
				return "<a href='"+data+"'>"+data+"</a>";
			}
		}, {
			colkey : "type",
			name : "广告类型",
			renderData : function(rowindex,data, rowdata, column) {
				var type;
				if(data==1){
					type="首页轮播图";
				}else if(data==2){
					type="新品推荐";
				}else if(data==3){
					type="热销产品";
				}else{
					type="特价产品";
				}
				return type;
			}
		}, {
			colkey : "createTime",
			name : "创建时间",
			renderData : function(rowindex,data, rowdata, column) {
				if(data){
					return new Date(data).format("yyyy-MM-dd");
				}else{
					return "";
				}
			}
		}, {
			colkey : "modifyTime",
			name : "创建时间",
			renderData : function(rowindex,data, rowdata, column) {
				if(data){
					return new Date(data).format("yyyy-MM-dd");
				}else{
					return "";
				}
			}
		}],
		jsonUrl : rootPath + '/advert/selectList.shtml',
		checkbox : true,
		serNumber : true,
		checkValue : "aId"
	});
	$("#search").click("click", function() {// 绑定查询按扭
		var searchParams = $("#searchForm").serializeJson();// 初始化传参数
		grid.setOptions({
			data : searchParams
		});
	});
	$("#addAdvert").click("click", function() {
		addAdvert();
	});
	$("#editAdvert").click("click", function() {
		editAdvert();
	});
	$("#delAdvert").click("click", function() {
		delAdvert();
	});
});
function editAdvert() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		layer.msg("只能选中一个");
		return;
	}
	pageii = layer.open({
		title : "编辑",
		type : 2,
		area : [ "800px", "80%" ],
		content : rootPath + '/advert/addOrEdit?id=' + cbox
	});
}
function addAdvert() {
	pageii = layer.open({
		title : "新增",
		type : 2,
		area : [ "800px", "80%" ],
		content : rootPath + '/advert/addOrEdit'
	});
}
function delAdvert() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox == "") {
		layer.msg("请选择删除项！！");
		return;
	}
	layer.confirm('是否删除？', function(index) {
		var url = rootPath + '/advert/deleteEntity.shtml';
		var s = CommnUtil.ajax(url, {
			ids : cbox.join(",")
		}, "json");
		if (s == "success") {
			layer.msg('删除成功');
			grid.loadData();
		} else {
			layer.msg('删除失败');
		}
	});
}